import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		String choice;
		String[] result =new String[30];
		int[] array =new int[30];
		int a=input.nextInt();
		String[] ID=new String[a];
		int j,t;
		String t1;
		for(int i=0;i<a;i++){
			 ID[i]=input.next();
			}
		while(input.hasNextLine()){
			choice=input.next();
			if(choice.equals("sort1")){
				for(int i=0;i<a;i++){
					result[i]=getbirth ( ID[i]);
			       }
				for(int i=0;i<a;i++){
					array[i]=Integer.parseInt(ID[i].substring(6,14));
			       }
				Arrays.sort(array);
				for(int i = 0;i<a;i++){
					System.out.println(result[i]);
				}
			
			   }
			else if(choice.equals("sort2")){
				for(int i=0;i<a;i++){
					array[i]=Integer.parseInt(ID[i].substring(6,14));
			       }
				for(int i=0;i<a-1;i++)
			    {
			        for(j=0;j<a-i-1;j++)
			        {
			            if(array[j+1]<array[j])
			            {
			              t=array[j+1];
			              array[j+1]=array[j];
			              array[j]=t;
			              t1=ID[j+1];
			              ID[j+1]=ID[j];
			              ID[j]=t1;
			             }
			        }
				
			}
				for(int i = 0;i<a;i++){
					System.out.println(ID[i]);
				}
			}
			else{
				System.out.println("exit");
		}
		}
		   
		
	}
	private static String getbirth (String ID) {
		
			String Year = ID.substring(6,10);
			String Month = ID.substring(10,12);
			String Day = ID.substring(12,14);
			return(Year+"-"+Month+"-"+Day);		
	}
	
		
	}


